package com.github.nuggets.controller;


import com.github.nuggets.entity.File;
import com.github.nuggets.model.pojo.LoginAccountInfo;
import com.github.nuggets.model.result.BaseResult;
import com.github.nuggets.service.FileService;
import com.github.nuggets.util.LoginAccountUtils;
import com.github.nuggets.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Optional;

/**
 * @description:
 * @author: wangsj
 * @date: 2022/3/17 1:05 下午
 */
@RestController()
public class FileController {

    @Autowired
    private FileService fileService;


    /**
     * 获取文件
     *
     * @param id
     * @return
     */
    @GetMapping("files/{id}")
    @ResponseBody
    public ResponseEntity<Object> serveFile(@PathVariable String id) {

        Optional<File>  file = fileService.getFileById(id);

        if (file.isPresent()) {
            return ResponseEntity
                    .ok()
                    .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; fileName=\"" + file.get().getName() + "\"")
                    .header(HttpHeaders.CONTENT_TYPE, "application/octet-stream")
                    .header(HttpHeaders.CONTENT_LENGTH, file.get().getSize() + "")
                    .header("Connection", "close")
                    .body(file.get().getContent());
        } else {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body("File was not fount");
        }

    }

    /**
     * 在线显示文件
     *
     * @param id
     * @return
     */
    @GetMapping("/view/{id}")
    @ResponseBody
    public ResponseEntity<Object> serveFileOnline(@PathVariable String id) {

        Optional<File> file = fileService.getFileById(id);


        if (file.isPresent()) {
            return ResponseEntity
                    .ok()
                    .header(HttpHeaders.CONTENT_DISPOSITION, "fileName=\"" + file.get().getName() + "\"")
                    .header(HttpHeaders.CONTENT_TYPE, file.get().getContentType())
                    .header(HttpHeaders.CONTENT_LENGTH, file.get().getSize() + "")
                    .header("Connection", "close")
                    .body(file.get().getContent());
        } else {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body("File was not fount");
        }

    }

    /**
     * 上传接口
     *
     * @param file
     * @return
     */
    @PostMapping("/nuggets/upload")
    @ResponseBody
    public BaseResult<String> handleFileUpload(@RequestPart("file") MultipartFile file, OAuth2Authentication auth) {

        LoginAccountInfo loginAccount = LoginAccountUtils.getLoginAccount(auth);

        return BaseResult.success(fileService.saveFile(loginAccount.getId(),file).getId());


    }

    /**
     * 分页查询文件
     *
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @GetMapping("/nuggets/files")
    @ResponseBody
    public BaseResult<List<File>> listFilesByPage(@RequestParam("pageIndex") int pageIndex, @RequestParam("pageSize") int pageSize, OAuth2Authentication auth) {

        LoginAccountInfo loginAccount = LoginAccountUtils.getLoginAccount(auth);

        return BaseResult.success(fileService.listFilesByPage(loginAccount.getId(), pageIndex, pageSize));


    }
}
